package br.com.easyrhour3x.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import br.com.easyrhour3x.hDAO.SessaoFactory;
import br.com.easyrhour3x.javamail.SendMail;
import br.com.easyrhour3x.model.Usuario;

public class Autenticacao {

	private Session session;

	public boolean validarLogin(String login, String senha) {
		try {
			session = SessaoFactory.getSessionFactory().openSession();
			Query q = session.getNamedQuery("Usuario.validarLogin");
			q.setString("loginParam", login);
			q.setString("senhaParam", senha);
			if (q.list().size() > 0) {
				List<Usuario> lista = new ArrayList<>(q.list());
				SendMail.EMAIL_FROM = lista.get(0).getEmail();
				SendMail.EMAIL_SENHA = lista.get(0).getSenha();
				return true;
			}
		} catch (HibernateException e) {
			throw new HibernateException("Falha de consulta no BD: ", e);
		} finally {
			session.close();
		}
		return false;
	}

}
